package com.sxj.corejava.code09_集合.set;

/**
 * @author 石小俊
 * @date 2025年02月19日
 */
public class User implements Comparable<User> {
    private String username;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(User user) {
        // 按照年龄升序排序
        if (this.age > user.getAge()) {
            return 1;
        } else if (this.age < user.getAge()) {
            return -1;
        } else {
            // 如果两者年龄相等,我们按照用户名长度进行降序排序
            if (this.username.length() > user.getUsername().length()) {
                return -1;
            } else if (this.username.length() < user.getUsername().length()) {
                return 1;
            }
        }
        return 0;
    }
}
